package algorithm.leetcode.I101to200;

import algorithm.leetcode.ListNode;

public class Q141 {

    public boolean hasCycle(ListNode head) {
        if (head == null) return false;

        ListNode fast = head.next, slow = head;

        while (fast != null && fast.next != null) {
            if (fast == slow) return true;

            fast = fast.next.next;
            slow = slow.next;
        }
        return false;
    }

    public static void main(String[] args) {
        ListNode listNode1 = new ListNode(1);
        ListNode listNode2 = new ListNode(2);
        ListNode listNode3 = new ListNode(3);
        ListNode listNode4 = new ListNode(4);
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode2;

        Q141 tzs = new Q141();
        System.out.println(tzs.hasCycle(listNode1));
    }

}
